<?php

class ExpressController extends BaseController
{
	
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl', // perform access control for CRUD operations
		);
	}
	
	/**
	 * 增加发货地址
	 * */
	public function actionAddressCreate()
	{
		$model=new MemAddres();
	
		// 保存地址
		if(isset($_POST['MemAddres']))
		{
			$model->attributes=$_POST['MemAddres'];
			if(empty($model->zar_id))
				$model->zar_id = $_POST['city_id'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}
		
		//出现错误，跳转到编辑页面
		$address = new MemAddres();
		$address->unsetAttributes();
		$address->zm_id = Company::model()->getCmpUserId();
		$this->render('address_update',array(
				'address'=>$address,
				'addressModel'=>$model,
		));
	}
	
	/**
	 * 修改收货，发货地址
	 */
	public function actionAddressUpdate()
	{
		$id = isset($_GET['id'])?(int)$_GET['id']:$_POST['MemAddres']['zma_id'];
		$model = MemAddres::model()->findByPk($id);

		//设置默认收货，发货地址
		if(isset($_GET['set'])){
			if($_GET['set']=='default'){
				$model->zma_default = '1';
			}elseif($_GET['set']=='default_sent'){
				$model->zma_default_sent = '1';
			}
			if($model->save()){
				$this->render('../index/success',array(
						'message'=>'恭喜，默认地址设置成功！',
						'links'=>array(
								array('返回物流管理页面',$this->pre_module_url.'/express/admin'),
						),
				));
				exit;
			}
		}
	
		if(isset($_POST['MemAddres']))
		{
			$model->attributes=$_POST['MemAddres'];
			if(empty($model->zar_id))
				$model->zar_id = $_POST['city_id'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}
	
		$address = new MemAddres();
		$address->unsetAttributes();
		$address->zm_id = Company::model()->getCmpUserId();
		
		$this->render('address_update',array(
				'address'=>$address,
				'addressModel'=>$model,
		));
		exit;
	}
	
	/**
	 * 删除收货地址
	 */
	public function actionAddressDelete($id)
	{
		MemAddres::model()->findByPk($id)->delete();
		//成功跳转
		$this->redirect(array('admin'));
	}
	
	/**
	 * 增加运费模板
	 */
	public function actionFreightCreate()
	{
		$model=new Express;
		$model->zm_id = Company::model()->getCmpUserId();
		
		if(isset($_POST['Express']))
		{
			$model->attributes=$_POST['Express'];
			$model->save();
			$freightLog = $_POST['freight'];
			foreach($freightLog['zel_type'] as $key=>$val){
				$freight = new ExpressLog();
				$freight->ze_id = $model->ze_id;
				$freight->zel_type = $val;
				$freight->zel_start_num = $freightLog['zel_start_num'][$key];
				$freight->zel_start_price = $freightLog['zel_start_price'][$key];
				$freight->zel_add_num = $freightLog['zel_add_num'][$key];
				$freight->zel_add_price = $freightLog['zel_add_price'][$key];
				$freight->zel_area = $freightLog['zel_area'][$key];
				$freight->save();
			}
			//成功跳转
			$this->redirect(array('admin'));
		}
	
		$this->render('freight_create',array(
				'model'=>$model,
		));
	}
	/**
	 * 修改运费模板
	 */
	public function actionFreightUpdate()
	{
		$id = isset($_GET['id'])?$_GET['id']:$_POST['Express']['ze_id'];
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Express']))
		{
			$model->attributes=$_POST['Express'];
			$model->save();
			$freightLog = $_POST['freight'];
			
			$criteria=new CDbCriteria;
			$criteria->compare('ze_id',$id);
			ExpressLog::model()->deleteAll($criteria);
			foreach($freightLog['zel_type'] as $key=>$val){
				$freight = new ExpressLog();
				$freight->ze_id = $model->ze_id;
				$freight->zel_type = $val;
				$freight->zel_start_num = $freightLog['zel_start_num'][$key];
				$freight->zel_start_price = $freightLog['zel_start_price'][$key];
				$freight->zel_add_num = $freightLog['zel_add_num'][$key];
				$freight->zel_add_price = $freightLog['zel_add_price'][$key];
				$freight->zel_area = $freightLog['zel_area'][$key];
				if(empty($freight->zel_area))
					$freight->zel_default = 2;//默认
				$freight->save();
			}
			//成功跳转
			$this->redirect(array('admin'));
		}

		$this->render('freight_update',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除运费模板
	 */
	public function actionFreightDelete($id)
	{
		$criteria=new CDbCriteria;
		$criteria->compare('ze_id',$id);
		
		Express::model()->deleteAll($criteria);
		ExpressLog::model()->deleteAll($criteria);
		
		//成功跳转
		$this->redirect(array('admin'));
	}


	/**
	 * 物流管理
	 */
	public function actionAdmin()
	{
		//从快递100网站获取物流公司列表。并存儲到數據庫
/*		$url=("http://www.kuaidi100.com/");
		$str=file_get_contents($url);
		preg_match_all("/selectCompany\(\'([A-Za-z0-9]+)\'\,this\)\;return false\;\"><span>([\x{4e00}-\x{9fa5}A-Za-z0-9]+)<\/span>/u",$str,$tea);
		foreach($tea[0] as $key=>$val){
			$criteria=new CDbCriteria;
			$criteria->compare('zec_com',$tea[1][$key]);
			if(!ExpressCom::model()->exists($criteria)){
				$express = new ExpressCom();
				$express->zec_title = $tea[2][$key];
				$express->zec_com = $tea[1][$key];
				$express->save();
			}
		}
*/	
		
		$model=new Express('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = Company::model()->getCmpUserId();
		
		$address = new MemAddres();
		$address->unsetAttributes();
		$address->zm_id = Company::model()->getCmpUserId();
		
		$this->render('admin',array(
			'address'=>$address,
			'model'=>$model,
		));
	}

	/**
	 * 运费模板管理
	 */
	public function actionExpressTemplate(){
		$model=new Express('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = Company::model()->getCmpUserId();

		$this->render('admin_template',array(
				'model'=>$model,
		));
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Express::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='express-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
